/*
 * Copyright 2010-2011 ESunny.com All right reserved. This software is the confidential and proprietary information of
 * ESunny.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with ESunny.com.
 */
package com.xjt.query.action;

import java.util.List;
import java.util.Map;

import com.xjt.common.StringUtils;
import com.xjt.query.dao.interf.DisplayPlusInfoDao;
import com.xjt.query.dao.pojo.DisplayPlusInfo;

/**
 * 根据表名获取表中的数据
 * 
 * @author Jet Xu 2012-7-27 上午10:02:51
 */
public class QueryListSimpleAction extends QueryListAbstractAction {

    private static final long serialVersionUID = -6641356745357437515L;

    /** uri后缀 */
    private static final String       URI_SUFFIX = ".htm";

    private DisplayPlusInfoDao        displayPlusInfoDao;

    private List<Map<String, Object>> dataList;

    public String queryList() {
        dataList = this.queryData();
        return "success";
    }

    @Override
    protected String getKey() {
        String key = fetchKeyFromUri(this.getRequest().getRequestURI(), "query_");
        return key;
    }

    @Override
    protected String getQuerySql() {
        String querySql = super.getQuerySql();
        DisplayPlusInfo plusInfo = displayPlusInfoDao.selectInfoByKey(this.getKey());
        String orderBy = "";
        if (null == plusInfo) {
            orderBy = "";
        } else {
            orderBy = plusInfo.getOrderby() == null ? "" : plusInfo.getOrderby();
            String selectSql = plusInfo.getSelectSql();
            if (StringUtils.isNotBlank(selectSql)) {
                querySql = selectSql;
            }
        }
        return querySql.replace(ORDERBY_REPLACE_STR, orderBy);
    }

    /**
     * 获取需要查询的表名
     * 
     * @return
     */
    public String fetchKeyFromUri(String uri, String prefix) {
        if (StringUtils.isBlank(uri)) {
            return null;
        }
        int idx = uri.lastIndexOf("/");
        String lastUri = uri;
        if (idx >= 0) {
            lastUri = uri.substring(idx + 1);
        }
        idx = lastUri.indexOf("?");
        if (idx >= 0) {
            lastUri = lastUri.substring(0, idx);
        }
        if (lastUri.startsWith(prefix) && lastUri.endsWith(URI_SUFFIX)) {
            int staIdx = prefix.length();
            int endIdx = lastUri.length() - URI_SUFFIX.length();
            return lastUri.substring(staIdx, endIdx);
        }

        return null;
    }

    /**
     * @return the dataList
     */
    public List<Map<String, Object>> getDataList() {
        return dataList;
    }

    /**
     * @param dataList the dataList to set
     */
    public void setDataList(List<Map<String, Object>> dataList) {
        this.dataList = dataList;
    }

    /**
     * @param displayPlusInfoDao the displayPlusInfoDao to set
     */
    public void setDisplayPlusInfoDao(DisplayPlusInfoDao displayPlusInfoDao) {
        this.displayPlusInfoDao = displayPlusInfoDao;
    }

}
